using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaHuan.Common;
using BaHuan.Entities;
/*
 * For each DB unit, normally tables, we create a DAO class for it
 * this will enalbe us to do some basic tasks for a DB unit
 * such as Save, Delete, GetAll, GetById....
 * 
 * */
namespace BaHuan.DataLayer
{
	
	class PhieuChiTienDAO:BaseDAO
	{
	
		// save into DB, @id = -1: insert else, update
		public ResultObj SavePhieuChiTien(PhieuChiTien phieuchitien)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spAddPhieuChiTien", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spAddPhieuChiTien";
			SqlParameter param = new SqlParameter();
			param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, phieuchitien.Id);
			cmd.Parameters.Add(param);
            param = CreateParameter("@Prefix", DbType.String, ParameterDirection.Input, phieuchitien.Prefix);
            cmd.Parameters.Add(param);
            param = CreateParameter("@OrderNumber", DbType.String, ParameterDirection.Input, phieuchitien.OrderNumber);
            cmd.Parameters.Add(param);
            param = CreateParameter("@MaSo", DbType.String, ParameterDirection.Input, phieuchitien.MaSo);
            cmd.Parameters.Add(param);
            param = CreateParameter("@History", DbType.String, ParameterDirection.Input, phieuchitien.History);
            cmd.Parameters.Add(param);
			param = CreateParameter("@NgayThang", DbType.DateTime, ParameterDirection.Input, phieuchitien.NgayThang);
			cmd.Parameters.Add(param);
            param = CreateParameter("@NgayThucChi", DbType.DateTime, ParameterDirection.Input, phieuchitien.NgayThucChi);
            cmd.Parameters.Add(param);
			param = CreateParameter("@IdChiNhanhCongTy", DbType.Int32, ParameterDirection.Input, phieuchitien.IdChiNhanhCongTy);
			cmd.Parameters.Add(param);
			param = CreateParameter("@IdNhapHang", DbType.Int32, ParameterDirection.Input, phieuchitien.IdNhapHang);
			cmd.Parameters.Add(param);
			param = CreateParameter("@IdMuaHang", DbType.Int32, ParameterDirection.Input, phieuchitien.IdMuaHang);
			cmd.Parameters.Add(param);
			param = CreateParameter("@DoiTuong", DbType.String, ParameterDirection.Input, phieuchitien.DoiTuong);
			cmd.Parameters.Add(param);
			param = CreateParameter("@LyDo", DbType.String, ParameterDirection.Input, phieuchitien.LyDo);
			cmd.Parameters.Add(param);
			param = CreateParameter("@NguoiLap", DbType.String, ParameterDirection.Input, phieuchitien.NguoiLap);
			cmd.Parameters.Add(param);
			param = CreateParameter("@NguoiNhanTien", DbType.String, ParameterDirection.Input, phieuchitien.NguoiNhanTien);
			cmd.Parameters.Add(param);
			param = CreateParameter("@SoTien", DbType.Int64, ParameterDirection.Input, phieuchitien.SoTien);
			cmd.Parameters.Add(param);
            param = CreateParameter("@ChungTuKemTheo", DbType.String, ParameterDirection.Input, phieuchitien.ChungTuKemTheo);
            cmd.Parameters.Add(param);
            param = CreateParameter("@IsDelete", DbType.Int32, ParameterDirection.Input, phieuchitien.IsDelete);
            cmd.Parameters.Add(param);
			param = CreateParameter("@ReturnVal", DbType.String, ParameterDirection.ReturnValue, result.ReturnCode);
			cmd.Parameters.Add(param);
			conObj.Open();
			cmd.ExecuteNonQuery();
			conObj.Close();
			result.ReturnCode = Convert.ToInt32(cmd.Parameters["@ReturnVal"].Value);
			
			return result;
		}
		
		// Get all PhieuChiTien in DB with IsDelete = 0
		public ResultObj GetAllPhieuChiTien( )
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<PhieuChiTien> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllPhieuChiTien", CommandType.StoredProcedure);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			    lst = new List<PhieuChiTien>();
			    PhieuChiTien item = null;
			    // reader all data from DB one by one
			    while (reader.Read())
			    {
                    item = new PhieuChiTien();
			        item.Id = Convert.ToInt32(reader["Id"]);
                    item.Prefix = Convert.ToString(reader["Prefix"]);
                    item.OrderNumber = Convert.ToString(reader["OrderNumber"]);
                    item.MaSo = Convert.ToString(reader["MaSo"]);
                    item.History = Convert.ToString(reader["History"]);
			        item.NgayThang = Convert.ToDateTime(reader["NgayThang"]);
                    item.NgayThucChi = Convert.ToDateTime(reader["NgayThucChi"]);
			        item.IdChiNhanhCongTy = Convert.ToInt32(reader["IdChiNhanhCongTy"]);
			        item.IdNhapHang = Convert.ToInt32(reader["IdNhapHang"]);
			        item.IdMuaHang = Convert.ToInt32(reader["IdMuaHang"]);
			        item.DoiTuong = Convert.ToString(reader["DoiTuong"]);
			        item.LyDo = Convert.ToString(reader["LyDo"]);
			        item.NguoiLap = Convert.ToString(reader["NguoiLap"]);
			        item.NguoiNhanTien = Convert.ToString(reader["NguoiNhanTien"]);
			        item.SoTien = Convert.ToInt64(reader["SoTien"]);
                    item.ChungTuKemTheo = Convert.ToString(reader["ChungTuKemTheo"]);
                    item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			        lst.Add(item);
			    }
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Get all PhieuChiTien in DB with condition
		public ResultObj GetAllPhieuChiTienByID(PhieuChiTien phieuchitien)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<PhieuChiTien> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllPhieuChiTienByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, phieuchitien.Id);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<PhieuChiTien>();
			PhieuChiTien item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new PhieuChiTien();
			item.Id = Convert.ToInt32(reader["Id"]);
            item.Prefix = Convert.ToString(reader["Prefix"]);
            item.OrderNumber = Convert.ToString(reader["OrderNumber"]);
            item.MaSo = Convert.ToString(reader["MaSo"]);
            item.History = Convert.ToString(reader["History"]);
			item.NgayThang = Convert.ToDateTime(reader["NgayThang"]);
            item.NgayThucChi = Convert.ToDateTime(reader["NgayThucChi"]);
			item.IdChiNhanhCongTy = Convert.ToInt32(reader["IdChiNhanhCongTy"]);
			item.IdNhapHang = Convert.ToInt32(reader["IdNhapHang"]);
			item.IdMuaHang = Convert.ToInt32(reader["IdMuaHang"]);
			item.DoiTuong = Convert.ToString(reader["DoiTuong"]);
			item.LyDo = Convert.ToString(reader["LyDo"]);
			item.NguoiLap = Convert.ToString(reader["NguoiLap"]);
			item.NguoiNhanTien = Convert.ToString(reader["NguoiNhanTien"]);
			item.SoTien = Convert.ToInt64(reader["SoTien"]);
            item.ChungTuKemTheo = Convert.ToString(reader["ChungTuKemTheo"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Delete a PhieuChiTien out of DB with condition
		public ResultObj DeletePhieuChiTien(PhieuChiTien phieuchitien)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spDeletePhieuChiTien", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spDeletePhieuChiTien";
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, phieuchitien.Id);
			cmd.Parameters.Add(param);
			conObj.Open();
			result.ReturnCode = cmd.ExecuteNonQuery();
			conObj.Close();
			
			return result;
		}
					
	}
}
